package com.sihan.swvp.action.sys;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sihan.swvp.action.BaseAction;
import com.sihan.swvp.common.CommonUtil;
import com.sihan.swvp.entity.sys.Role;
import com.sihan.swvp.service.sys.LogService;
import com.sihan.swvp.service.sys.PrivRoleService;


@Controller
@RequestMapping("/priv/role")
public class PrivRoleAction extends BaseAction{
	
	@Autowired
	PrivRoleService privRoleService;
	@Autowired
	public LogService logService;
	
	@ResponseBody
	@RequestMapping(value="/getRole")
	public Object getRole(){
		
		String cd = getParameter("cd");
		List<Map<String, Object>> list = privRoleService.getRole(cd);
		return list;
	} 
	
	
	@ResponseBody
	@RequestMapping(value="/addRole")
	public Object addRole(@ModelAttribute Role role){
		try {
			if(CommonUtil.isEmptyString(role.getId())){
				role.setEnbale(1);
				if(privRoleService.getRoleNameByName(role.getName()) > 0){
					logService.addUserLog("添加角色", "角色", getLoginUser().getId(), "角色："+role.getName()+"已存在",getRemoteAddr(),0,1);
					return successMsg("次角色名称已存在，请重新输入。");
				}
				privRoleService.addRole(role);
				logService.addUserLog("添加角色", "角色", getLoginUser().getId(), "添加角色："+role.getName(),getRemoteAddr(),1,1);
			}
			else{
				privRoleService.upateRole(role);
				logService.addUserLog("修改角色", "角色", getLoginUser().getId(), "修改角色："+role.getName(),getRemoteAddr(),1,1);
			}
			
			return successMsg("保存成功");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return failMsg("服务器异常");
		}
	} 
	
	@ResponseBody
	@RequestMapping(value="/deleteRole")
	public Object deleteRole(@RequestParam String id,String name){
		try {
			privRoleService.deleteRole(id);
			logService.addUserLog("删除角色", "角色", getLoginUser().getId(), "删除角色："+name,getRemoteAddr(),1,1);
			return successMsg("");
		} catch (Exception e) {
			e.printStackTrace();
			logService.addUserLog("删除角色", "角色", getLoginUser().getId(), "删除角色："+name,getRemoteAddr(),0,1);
			return failMsg("服务器异常");
		}
	}
	
	@ResponseBody
	@RequestMapping(value="/getMenu")
	public List<Map<String,Object>> getMenu(@RequestParam String role,@RequestParam String node){
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("role", role);
		params.put("node", node);
		params.put("user", getLoginUser().getId());
		List<Map<String,Object>> list = privRoleService.getMenuTree(params);
		for (Map<String, Object> map : list) {
			getCh(map,role);
		}
		return list;
	} 
	
	private void getCh(Map<String, Object> map,String role){
		if("0".equals(CommonUtil.o2string(map.get("leaf")))){
			Map<String,Object> params = new HashMap<String,Object>();
			params.put("role", role);
			params.put("node", map.get("id"));
			params.put("user", getLoginUser().getId());
			List<Map<String,Object>> list = privRoleService.getMenuTree(params);
			for (Map<String, Object> map2 : list) {
				getCh(map2,role);
			}
			map.put("children", list);
		}
	}
	
	@ResponseBody
	@RequestMapping(value="/addPriv")
	public Object addPriv(@RequestParam String role,@RequestParam String ids){
		try {
			String[] idsarray = ids.split(",");
			if(ids.indexOf(",") > 0){
				List<Integer> idslist = new ArrayList<>();
				for(int i=0;i<idsarray.length;i++){
					if(Integer.parseInt(idsarray[i]) > 0){
						idslist.add(Integer.parseInt(idsarray[i]));
					}
				}
				int usedmenucount = privRoleService.getUsedMenuCount(idslist);
				if(usedmenucount > 0){
					return failMsg("你分配的菜单中存在已分配的菜单，请重新选择。");
				}else{
					privRoleService.deleteAllPriv(role);
					Map<String,Object> params = new HashMap<String,Object>();
					params.put("role", role);
					params.put("ids", idsarray);
					privRoleService.addPriv(params);
					logService.addUserLog("菜单分配", "菜单", getLoginUser().getId(), "角色："+role+"分配菜单",getRemoteAddr(),1,1);
				}
			}else{
				privRoleService.deleteAllPriv(role);
				logService.addUserLog("菜单分配", "菜单", getLoginUser().getId(), "角色："+role+"分配菜单",getRemoteAddr(),1,1);
			}
			return successMsg("");
		} catch (Exception e) {
			// TODO: handle exception
			logService.addUserLog("菜单分配", "菜单", getLoginUser().getId(), "角色："+role+"分配菜单",getRemoteAddr(),0,1);
			return failMsg("服务器异常");
		}
	} 
}
